/*
 * @Author: i@douxuefeng.cn
 * @Date: 2021-12-22 19:08:59
 * @LastEditTime: 2021-12-23 19:15:22
 * @LastEditors: i@douxuefeng.cn
 * @Description:
 * @FilePath: \go-im\ws\msg_handler.go
 */

package ws

import (
	"encoding/json"
	"time"

	"gitee.com/douxuefeng/go-im/params"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/neffos"
	"github.com/spf13/viper"
)

type handler struct {
	handlerParams
}

type handlerParams struct {
	uid  int64
	msg  params.ReceiveMsg
	conn *neffos.NSConn
	ctx  *context.Context
}

func NewHandler(data handlerParams) *handler {
	return &handler{
		data,
	}
}

func (h *handler) Deal() {
	user, ok := params.UserTokens.Load(h.uid)
	if !ok {
		return
	}
	u := user.(params.UserInfo)
	now := time.Now().Format(viper.GetString("timeFormat"))
	switch h.msg.Action {
	case params.ActionJoinRoom:
		if h.conn.Room(h.msg.Room) == nil {
			h.conn.JoinRoom(h.ctx, h.msg.Room)
			h.conn.Conn.Write(getMsg(params.SendMsg{
				Action: params.ActionJoinRoom,
				Room:   h.msg.Room,
				IsMe:   true,
				Msg: params.Message{
					MsgType: params.MsgTypeWord,
					Content: "加入房间成功",
					MsgAt:   now,
				},
			}))
			h.conn.Conn.Server().Broadcast(h.conn, getMsg(params.SendMsg{
				Action: params.ActionJoinRoom,
				Room:   h.msg.Room,
				IsMe:   false,
				Msg: params.Message{
					MsgType: params.MsgTypeWord,
					Content: u.Name + "进入房间",
					MsgAt:   now,
				},
			}))
			datas := NewLog(h.msg.Room).read()
			dataByte, _ := json.Marshal(iris.Map{
				"error_code": 0,
				"action":     params.ActionJoinRoomHistoryMsgs,
				"data":       datas,
			})
			h.conn.Conn.Write(neffos.Message{
				Body:     dataByte,
				IsNative: true,
			})
		}
	case params.ActionChatInRoom:
		if h.conn.Room(h.msg.Room) == nil {
			h.conn.Conn.Write(getMsg(params.SendMsg{
				Errorcode: params.ErrorWrongRoom,
				IsMe:      true,
				Msg: params.Message{
					MsgType: params.MsgTypeWord,
					Content: "你不在该房间内，不能在该房间聊天",
					MsgAt:   now,
				},
			}))
			return
		}
		go NewLog(h.msg.Room).write(params.LogMsg{
			Action: h.msg.Action,
			Room:   h.msg.Room,
			User:   &u,
			Msg: params.Message{
				MsgType: h.msg.Msg.MsgType,
				Content: h.msg.Msg.Content,
				MsgAt:   now,
			},
		})
		h.conn.Conn.Write(getMsg(params.SendMsg{
			Action: params.ActionChatInRoom,
			Room:   h.msg.Room,
			IsMe:   true,
			User:   &u,
			Msg: params.Message{
				MsgType: h.msg.Msg.MsgType,
				Content: h.msg.Msg.Content,
				MsgAt:   now,
			},
		}))
		h.conn.Conn.Server().Broadcast(h.conn, getMsg(params.SendMsg{
			Action: params.ActionChatInRoom,
			Room:   h.msg.Room,
			IsMe:   false,
			User:   &u,
			Msg: params.Message{
				MsgType: h.msg.Msg.MsgType,
				Content: h.msg.Msg.Content,
				MsgAt:   now,
			},
		}))
	case params.ActionLeftRoom:
		if h.conn.Room(h.msg.Room) == nil {
			return
		}
		h.conn.Conn.Write(getMsg(params.SendMsg{
			Action: params.ActionLeftRoom,
			Room:   h.msg.Room,
			IsMe:   true,
			Msg: params.Message{
				MsgType: params.MsgTypeWord,
				Content: "离开房间成功",
				MsgAt:   now,
			},
		}))
		h.conn.Room(h.msg.Room).Leave(h.ctx)
		h.conn.Conn.Server().Broadcast(h.conn, getMsg(params.SendMsg{
			Action: params.ActionLeftRoom,
			Room:   h.msg.Room,
			User:   &u,
			IsMe:   false,
			Msg: params.Message{
				MsgType: params.MsgTypeWord,
				Content: u.Name + "离开房间",
				MsgAt:   now,
			},
		}))
	}
}

func getMsg(data params.SendMsg) neffos.Message {
	body, _ := json.Marshal(data)
	neffosMsg := neffos.Message{
		Body:     body,
		IsNative: true,
		Room:     data.Room,
	}
	return neffosMsg
}
